<?php
namespace app\index\controller;

use app\index\model\Answer;
use app\index\validate\IndexValidator;
use think\Controller;
use think\Db;
use think\Request;
use app\index\model\Message;
class Index extends Controller
{

    public function checkLogin()
    {
        if(!session("?username"))
            $this->error('请先登录','User/login');
    }
    public function index()
    {
        $list = Db::view('user','user_id,username')
            ->view('message','message_id,create_time,content,update_time','user.user_id=message.user_id')
            ->order('update_time desc')
            ->paginate(6);//实现分页，参数代表每条里面几个数据
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function post()
    {
        $this->checkLogin();
        return $this->fetch();

    }
    public function myinfo()
    {
        $this->checkLogin();
        $list = Db::view('user','user_id,username')
            ->view('message','message_id,content,update_time','user.user_id=message.user_id')
            ->where('user_id',session("user_id"))
            ->paginate(6);//实现分页，参数代表每条里面几个数据
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function do_post(Request $request)
    {
        $this->checkLogin();
        $content = $request->param("content");
        $user_id = session('user_id');
        $message = new Message();
        $message->content = $content;
        $message->user_id = $user_id;
        $vali = new IndexValidator();
        if(!$vali->scene('need')->check($message))
            return $vali->getError();
        else{
        if($message->save())
            $this->success("发表成功","index");
        else
            $this->error("发表失败","post");
    }}
    public function delete(Request $request)
    {
        $id = $request->param('id');
        $message = Message::get($id);
        if($message->delete())
            $this->success("删除成功","index");
        else
            $this->error("删除失败","index");
    }
    public function change(Request $request)
    {
        $id = $request->param('id');
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function do_change(Request $request)
    {
        $id = $request->param('id');
        $content = $request->param('content');
        $message = new Message();
        $data = ["content"=>$content]; //保存要修改的内容
        $where = ["message_id"=>$id];//where保存条件
        if($message->save($data,$where))//调用模型类的save方法保存数据
            $this->success("更新成功","myinfo");
        else
            $this->error("更新失败","myinfo");


    }
    public function answer(Request $request)
    {
        $message_id = $request->param('id');
        $list = Db::view('user','user_id,username')
            ->view('message','message_id,content,update_time','message.user_id=user.user_id')
            ->where('message_id',$message_id)
            ->paginate(1);//实现分页，参数代表每条里面几个数据
        $this->assign('list',$list[0]);

        $list1 = Db::view('answer','answer,create_time,message_id,user_id,answer_id')
            ->view('message','message_id','message.message_id=answer.message_id')
            ->view('user','user_id,username','user.user_id=answer.user_id')
            ->where('message_id',$message_id)
            ->paginate(5);
        $this->assign('list1',$list1);
        return $this->fetch();
    }
    public function do_answer(Request $request)
    {   $this->checkLogin();
        $content = $request->param("content");
        $user_id = session('user_id');
        $message_id = $request->param('message_id');
        $message = new Answer();
        $message->answer = $content;
        $message->user_id = $user_id;
        $message->message_id = $message_id;
        $vali = new IndexValidator();
        if(!$vali->scene('aa')->check($message))
            return $vali->getError();
        else{
            if($message->save())
                $this->success("发表成功","answer?id=".$message_id);
            else
                $this->error("发表失败","answer?id=".$message_id);
        }

    }
    public function delete_answer(Request $request)
    {
        $id = $request->param('id');
        $message_id = $request->param('mes_id');
        $message = Answer::get($id);
        if($message->delete())
            $this->success("删除成功","answer?id=".$message_id);
        else
            $this->error("删除失败","answer?id=".$message_id);
    }

}
